package com.db.williamchart.renderer.executor;

import com.db.williamchart.extensions.Pair;

import com.db.williamchart.data.Frame;

import java.util.ArrayList;
import java.util.List;

public class DefineDataPointsClickableFrames {

    public List<Frame> invoke(Frame innerFrame, List<Pair<Float, Float>> datapointsCoordinates, int clickableRadius) {
        float halfWidthBetweenLabels =
                ((innerFrame.getRight() - innerFrame.getLeft()) / (datapointsCoordinates.size() - 1)) / 2;
        List<Frame> list = new ArrayList<>();
        for (int i = 0; i < datapointsCoordinates.size(); i++) {
            float left = datapointsCoordinates.get(i).first - clickableRadius;
            if (clickableRadius > halfWidthBetweenLabels) {
                left = datapointsCoordinates.get(i).first - halfWidthBetweenLabels;
            }
            float top = datapointsCoordinates.get(i).second - clickableRadius;
            float right = datapointsCoordinates.get(i).first + clickableRadius;
            if (clickableRadius > halfWidthBetweenLabels) {
                right = datapointsCoordinates.get(i).first + halfWidthBetweenLabels;
            }
            float bottom = datapointsCoordinates.get(i).second + clickableRadius;

            Frame frame = new Frame(left, top, right, bottom);
            list.add(frame);
        }
        return list;
    }
}
